﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Data.Attributes;
    using Shanda.DevFx.Data.Utils;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class RelationPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected Button btnSubmit;
        protected CheckBox cbxExecute;
        protected CheckBox cbxGrant;
        protected CheckBox cbxList;
        [Column("PermissionNo", WriteOnly=true)]
        protected DropDownList ddlPermissionNo;
        [Column("ResNo", ReadOnly=true)]
        protected DropDownList ddlResNo;
        [Column("RoleNo", WriteOnly=true)]
        protected DropDownList ddlRoleNo;
        protected Label lblMessage;
        private bool newItem;
        private int relationId;
        protected RegularExpressionValidator revPriority;
        protected RequiredFieldValidator rfvPermissionNo;
        protected RequiredFieldValidator rfvPriority;
        protected RequiredFieldValidator rfvRoleNo;
        [Column("Priority", ColumnType=typeof(int))]
        protected TextBox txtPriority;
        [Column("Remark")]
        protected TextBox txtRemark;
        protected ValidationSummary vsRelation;

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!base.get_IsValid())
            {
                this.lblMessage.set_Text("请正确填写表单");
            }
            else
            {
                Relation relation;
                if (this.newItem)
                {
                    relation = Securitor.RelationManager.CreateItem();
                    relation.RoleNo = this.ddlRoleNo.get_SelectedValue();
                    relation.PermissionNo = this.ddlPermissionNo.get_SelectedValue();
                }
                else
                {
                    RelationCollection relations = Securitor.Authentication.CurrentRelations.SelectByRelationID(new int[] { this.relationId });
                    if (relations.Count != 1)
                    {
                        WebHelper.ShowMessage("没找到此权限关系，或者您没有权限操作", "history.back()", true, true);
                    }
                    relation = relations[0];
                }
                DataTransfer.TransWebFormDataToEntityProperty(this, relation);
                RelationStatusFlags flags = 0;
                if (this.cbxExecute.get_Checked())
                {
                    flags |= RelationStatusFlags.Execute;
                }
                if (this.cbxList.get_Checked())
                {
                    flags |= RelationStatusFlags.List;
                }
                if (this.cbxGrant.get_Checked())
                {
                    flags |= RelationStatusFlags.Grant;
                }
                if (flags != relation.StatusFlags)
                {
                    relation.StatusFlags = flags;
                }
                IAOPResult result = relation.Save();
                string resultDescription = "成功保存";
                if (result.IsFailed)
                {
                    resultDescription = result.ResultDescription;
                }
                this.lblMessage.set_Text(resultDescription);
            }
        }

        private void ddlPermissionNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            string argValue = this.ddlPermissionNo.get_SelectedValue();
            if (!Checker.CheckEmptyString("PermissionNo", argValue, false))
            {
                string resType = Securitor.PermissionManager.GetItem(argValue).ResType;
                this.ddlResNo.set_DataTextField("ResName");
                this.ddlResNo.set_DataValueField("ResNo");
                this.ddlResNo.set_DataSource(Securitor.ResManager.ListItem(resType));
                this.ddlResNo.DataBind();
                this.ddlResNo.set_Enabled(!Checker.CheckEmptyString("ResType", resType, false));
            }
        }

        private void InitializeComponent()
        {
            this.ddlPermissionNo.add_SelectedIndexChanged(new EventHandler(this.ddlPermissionNo_SelectedIndexChanged));
            this.btnSubmit.add_Click(new EventHandler(this.btnSubmit_Click));
            base.add_Load(new EventHandler(this.Page_Load));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            string argValue = base.get_Request().get_Params()["RelationID"];
            if (Checker.CheckEmptyString("RelationID", argValue, false))
            {
                this.newItem = true;
            }
            else
            {
                try
                {
                    this.relationId = int.Parse(argValue);
                }
                catch
                {
                    this.newItem = true;
                }
            }
            if (!base.get_IsPostBack())
            {
                this.ddlRoleNo.set_DataTextField("Title");
                this.ddlRoleNo.set_DataValueField("RoleNo");
                this.ddlRoleNo.set_DataSource(Securitor.RoleManager.ListItem());
                this.ddlRoleNo.DataBind();
                string str2 = base.get_Request().get_QueryString()["RoleNo"];
                if (!Checker.CheckEmptyString("RoleNo", str2, false))
                {
                    this.ddlRoleNo.set_SelectedValue(str2);
                }
                this.ddlPermissionNo.set_DataTextField("IndentTitle");
                this.ddlPermissionNo.set_DataValueField("PermissionNo");
                this.ddlPermissionNo.set_DataSource(Securitor.PermissionManager.ListItem());
                this.ddlPermissionNo.DataBind();
                string str3 = base.get_Request().get_QueryString()["PermissionNo"];
                if (!Checker.CheckEmptyString("PermissionNo", str3, false))
                {
                    this.ddlPermissionNo.set_SelectedValue(str3);
                }
                if (!this.newItem)
                {
                    RelationCollection relations = Securitor.Authentication.CurrentRelations.SelectByRelationID(new int[] { this.relationId });
                    if (relations.Count != 1)
                    {
                        WebHelper.ShowMessage("没找到此权限关系，或者您没有权限操作", "history.back()", true, true);
                    }
                    Relation entity = relations[0];
                    DataTransfer.TransEntityPropertyToWebFormData(entity, this);
                    this.ddlPermissionNo_SelectedIndexChanged(this, null);
                    this.ddlResNo.set_SelectedValue(entity.ResNo);
                    this.cbxExecute.set_Checked((entity.StatusFlags & RelationStatusFlags.Execute) != 0);
                    this.cbxList.set_Checked((entity.StatusFlags & RelationStatusFlags.List) != 0);
                    this.cbxGrant.set_Checked((entity.StatusFlags & RelationStatusFlags.Grant) != 0);
                    this.ddlRoleNo.set_Enabled(false);
                    this.ddlPermissionNo.set_Enabled(false);
                }
                else
                {
                    this.ddlPermissionNo_SelectedIndexChanged(this, null);
                }
            }
        }
    }
}

